
package com.yuan.graduate;

import com.yuan.application.BaseActivity;
import com.yuan.prefence.PreferenceKeys;
import com.yuan.unit.PreferenceUtil;
import com.yuan.view.LockPatternView;
import com.yuan.view.LockPatternView.Cell;
import com.yuan.view.LockPatternView.DisplayMode;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;

import java.util.List;

public class LockActivity extends BaseActivity implements
        LockPatternView.OnPatternListener {
    private Context mContext;
    private List<Cell> lockPattern;
    private LockPatternView lockPatternView;
    private boolean flag = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lock);
        mContext = this;
        initView();
    }

    private void initView() {
        // TODO Auto-generated method stub
        String patternString = PreferenceUtil.getInstance().getString(PreferenceKeys.LOCK_KEY,
                null);
        if (patternString == null) {
            finish();
            return;
        }
        lockPattern = LockPatternView.stringToPattern(patternString);
        lockPatternView = (LockPatternView) findViewById(R.id.lock_lock_pattern);
        lockPatternView.setOnPatternListener(LockActivity.this);
        try {
            flag = getIntent().getExtras().getBoolean("isAutoLogin");
        } catch (Exception e)
        {
            e.printStackTrace();
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {

            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onPatternStart() {
    }

    @Override
    public void onPatternCleared() {
    }

    @Override
    public void onPatternCellAdded(List<Cell> pattern) {
    }

    @Override
    public void onPatternDetected(List<Cell> pattern) {
        if (pattern.equals(lockPattern)) {
            Intent mIntent = new Intent(mContext, LoginActivity.class);
            mIntent.putExtra("isAutoLogin", flag);
            startActivity(mIntent);
        } else {
            lockPatternView.setDisplayMode(DisplayMode.Wrong);
            Toast.makeText(this, R.string.lockpattern_error, Toast.LENGTH_LONG)
                    .show();
        }

    }

}
